package com.example.proyyj.Controller;


import com.example.proyyj.Entity.Chinaware;
import com.example.proyyj.Entity.Collection;
import com.example.proyyj.Entity.ResultEntity;
import com.example.proyyj.Service.CollectionService;
import com.example.proyyj.Util.JSONTool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Collection/")//请求路径
public class CollectionController {
    @Resource
    CollectionService collectionService;
    ResultEntity resultEntity=null;


    /**
     * 是否收藏
     * @param userid
     * @param chinawareid
     * @return
     */
    @GetMapping("collection")
    private  String collection(@RequestParam("userid") int userid,@RequestParam("chinawareid") int chinawareid){
        resultEntity = new ResultEntity();

        try{
            Collection collection=collectionService.collection(userid, chinawareid);
            if (collection !=null){
                resultEntity.setCode(0);
                resultEntity.setMsg("已收藏！");
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("未收藏！");
            }

        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(999);
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }


    /**
     * 收藏用户
     * @param userid
     * @param chinawareid
     * @return
     */
    @PostMapping("addChinaware")
private String addchinaware(@RequestParam("userid") int userid,@RequestParam("chinawareid") int chinawareid){
    resultEntity = new ResultEntity();

    try{
        if (collectionService.addchiaware(userid,chinawareid)>0) {

            resultEntity.setCode("0");
            resultEntity.setMsg("添加成功！");
        }else {
            resultEntity.setCode(2);
            resultEntity.setMsg("收藏失败！");
        }



    }catch (Exception e){
        e.printStackTrace();
        resultEntity.setCode(999);
        resultEntity.setMsg("系统错误，请联系管理员");
    }
    return JSONTool.getJsonTool().toJson(resultEntity);
}

    /**
     * 取消收藏
     * @param userid
     * @param chinawareid
     * @return
     */
    @DeleteMapping("delchiaware")
    private String delchiaware(@RequestParam("userid") int userid,@RequestParam("chinawareid") int chinawareid){
        resultEntity = new ResultEntity();

        try{
            if (collectionService.delchiaware(userid,chinawareid)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功");
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("删除失败");
            }


        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }

        return JSONTool.getJsonTool().toJson(resultEntity);
    }




@GetMapping("lookChiaware")
    private  String lookchiaware(@RequestParam("userid") int userid){
    resultEntity = new ResultEntity();
        try{
            resultEntity = new ResultEntity();
            List<Collection> list =collectionService.lookchiawarelist(userid);
            if ( list !=null){
                resultEntity.setCode(0);
                resultEntity.setMsg("查看成功");
                resultEntity.setData(list);
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("查看失败");
            }

        }catch (Exception e){

            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }

    return JSONTool.getJsonTool().toJson(resultEntity);
}

@GetMapping("lookChiawareid")
    private String lookChiaware(@RequestParam("chinawareid") int chinawareid){
    resultEntity = new ResultEntity();
        try{
            Chinaware collection=collectionService.lookchiaware(chinawareid);
            if (collection !=null){
                resultEntity.setCode(0);
                resultEntity.setMsg("查询成功");
                resultEntity.setData(collection);
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("未查询成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }
    return JSONTool.getJsonTool().toJson(resultEntity);
}




}
